package com.xin.beauty.interceptor;

import com.xin.beauty.util.JwtUtil;
import com.xin.beauty.util.ThreadLocalUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Map;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拿到验证
        String token = request.getHeader("Authorization");
        //验证token
        try {
            Map<String, Object> map = JwtUtil.parseToken(token);
            //token里面的payload：id,name
            ThreadLocalUtil.set(map);
            //方行
            return true;
        } catch (Exception e) {
            //http响应状态码为401
            response.setStatus(401);
            return false;
        }
    }
}
